package com.tangyee.home_exercise_diet.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.tangyee.R;
import com.tangyee.home_blood_center.utils.DensityUtils;

/**
 * 圆形view,用于显示数据偏高、正常、偏低信息的圆形view
 */
public class CircleView extends View {

    private int mMeasureHeigth;
    private int mMeasureWidth;

    private Paint mArcPaint;
    private RectF mArcRectF;
    private float mSweepAngle;

    private int number;
    private int totalNum;

    private Paint backgroundPaint;

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CircleView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mMeasureWidth = MeasureSpec.getSize(widthMeasureSpec);
        mMeasureHeigth = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mMeasureWidth, mMeasureHeigth);
        initSize();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制弧线
        canvas.drawArc(mArcRectF, 270, 360f, false, backgroundPaint);
        canvas.drawArc(mArcRectF, 270, mSweepAngle, false, mArcPaint);
    }

    private void initSize() {
        float length = Math.min(mMeasureHeigth, mMeasureWidth);
        float d = mMeasureHeigth * 0.75f; // 直径

        float left = (mMeasureWidth - d)/2f; // 左
        float top = (mMeasureHeigth - d)/2f; // 上
        float right = mMeasureWidth - (mMeasureWidth - d)/2f; // 右
        float bottom = mMeasureHeigth - (mMeasureHeigth - d)/2f; // 下
        mArcRectF = new RectF(left, top, right, bottom); // 画圆（弧）的rect
        mArcPaint.setStrokeWidth((float) (length * 0.02)); // 弧的宽度
        backgroundPaint.setStrokeWidth((float) (length * 0.02)); // 弧宽
    }

    private void init(Context context, AttributeSet attrs) {

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleView);
        number = typedArray.getInt(R.styleable.CircleView_number, 0);
        totalNum = typedArray.getInt(R.styleable.CircleView_totalNum, 2000);
        int color = typedArray.getColor(R.styleable.CircleView_arcColor, Color.WHITE);
        //记得此处要recycle();
        typedArray.recycle();

        mSweepAngle = (number * 1f / totalNum) * 360f; // 弧形扫过的弧度
        mArcPaint = new Paint(); // 画弧的paint
        mArcPaint.setAntiAlias(true); // 抗锯齿
        mArcPaint.setColor(color); // 设置画弧度的颜色

        mArcPaint.setStyle(Style.STROKE); //。。。

        backgroundPaint = new Paint(); // 画背景的paint
        backgroundPaint.setAntiAlias(true); // 抗锯齿
        backgroundPaint.setColor(getResources().getColor(R.color.circle_bg2)); // 弧线颜色

        backgroundPaint.setStyle(Style.STROKE);
    }

    public void setDate(int n, int totalNum) {
        this.number = n;
        this.totalNum = totalNum;
        mSweepAngle = (number * 1f / totalNum) * 360f; // 弧形扫过的弧度
        this.invalidate();
    }

}

